//
//  XBBaseController.swift
//  Sina
//
//  Created by 周旭斌 on 2017/3/24.
//  Copyright © 2017年 周旭斌. All rights reserved.
//

import UIKit

typealias itemClickBlock = (() -> ())?;

var buttonActionKeyWords = "keyWord";

class XBBaseTableViewController: UIViewController {
    
    // MARK:存储属性
    var tableView = UITableView();
    
    var leftItemAction : itemClickBlock?;
    var rightItemAction : itemClickBlock?;
    
    // 添加导航栏左边的item及action
    func addLeftItemWith(_ normalImageName : String, highlightedImageName : String, title : String, itemAction : itemClickBlock) {
        let button = UIButton();
        button.frame = CGRect(x: 0, y: 0, width: 30, height: 30);
        // 需要设置按钮标题的时候再设置
        if (title as NSString).length > 0 {
            button.setTitle(title, for: UIControlState.normal);
            button.setTitle(title, for: UIControlState.highlighted);
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14);
            button.setTitleColor(UIColor.black, for: UIControlState.normal);
            button.setTitleColor(UIColor.black, for: UIControlState.highlighted);
        }
        if (normalImageName as NSString).length > 0 {
            button.setImage(UIImage.init(named: normalImageName), for: UIControlState.normal);
        }
        if (highlightedImageName as NSString).length > 0 {
            button.setImage(UIImage.init(named: highlightedImageName), for: UIControlState.highlighted);
        }
        button.addTarget(self, action: #selector(leftItemClick(button:)), for: UIControlEvents.touchUpInside);
        leftItemAction = itemAction;
        //            objc_setAssociatedObject(button, &buttonActionKeyWords, itemAction, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC);
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button);
    }
    
    // 添加右边导航栏item
    func addRightItemWith(_ normalImageName : String, highlightedImageName : String, title : String, itemAction : itemClickBlock) {
        let button = UIButton();
        button.frame = CGRect(x: 0, y: 0, width: 30, height: 30);
        // 需要设置按钮标题的时候再设置
        if (title as NSString).length > 0 {
            button.setTitle(title, for: UIControlState.normal);
            button.setTitle(title, for: UIControlState.highlighted);
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14);
            button.setTitleColor(UIColor.black, for: UIControlState.normal);
            button.setTitleColor(UIColor.black, for: UIControlState.highlighted);
        }
        if (normalImageName as NSString).length > 0 {
            button.setImage(UIImage.init(named: normalImageName), for: UIControlState.normal);
        }
        if (highlightedImageName as NSString).length > 0 {
            button.setImage(UIImage.init(named: highlightedImageName), for: UIControlState.highlighted);
        }
        button.addTarget(self, action: #selector(rightItemClick(button:)), for: UIControlEvents.touchUpInside);
        rightItemAction = itemAction;
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button);
    }
    
    // MARK:私有方法
    @objc private func leftItemClick(button : UIButton) {
        if let action = leftItemAction {
            action!();
        }
    }
    
    @objc private func rightItemClick(button : UIButton) {
        if let action = rightItemAction {
            action!();
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        // 设置tableView
        setUpTableView();
    }
    
    // MARK:设置ui
    private func setUpTableView() {
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.frame = ScreenBouds;
        tableView.tableFooterView = UIView();
        view.addSubview(tableView);
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

// MARK:tableViewDataSouce
extension XBBaseTableViewController : UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "baseCell");
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "baseCell");
        }
        cell?.textLabel?.text = "\(indexPath.row)";
        return cell!;
    }
}
